package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import com.alibaba.excel.EasyExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired//创建部门对象
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    //接收用户传递过来的分页参数值,封装到queryObject对象中,并查询所有部门数据存放的作用域中
    //因为创建了employeeQuery,所以下面的方法值也要改成EmployeeQuery
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo){
        Subject subject = SecurityUtils.getSubject();//这里只是演示效果

        System.out.println("判断用户是否有admin角色"+subject.hasRole("admin"));
        System.out.println("判断用户是否有hr"+subject.hasRole("hr"));


        //查询部门全部信息,这里的depts指向的是list.jsp中的foreach中的元素{depts}
        model.addAttribute("depts",departmentService.listAll());

    //model共享员工数据到页面上
        model.addAttribute("result",employeeService.query(qo));
        return "employee/list";
    }

    @RequestMapping("/input")
    //部门回显要通过他的id来回显,所以参数这个要添加一个id
    public String input(Model model,Long id){
        //判断id值是否为空
        if(id!=null){
            Employee employee = employeeService.get(id);
            //共享数据,这里的值要与input界面的相对应
            model.addAttribute("employee",employee);
        }
        //查询部门的数据,不然下拉框不弹出来
        model.addAttribute("depts",departmentService.listAll());
        //角色回显
        model.addAttribute("roles",roleService.listAll());
        return "employee/input";
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids){
        //判断是保存还是更新
        if(employee.getId()==null){
            employeeService.save(employee,ids);
        }else {
            employeeService.update(employee,ids);
        }
        //重定向返回到list页面
        return new JsonResult();
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        employeeService.batchDelete(ids);
        return new JsonResult();

    }
    /*
    用户远程验证用户名是否存在,引用插件valid:true,返回map类型
    返回boolean的类型 true代表验证通过(该用户不存在),false代表验证不通过,(用户名已经存在)
     */

    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(String name){
        Employee employee = employeeService.selectByName(name);
        //创建map对象
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("valid",employee==null);//判断是否为空
        return map;

    }
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //文件下载的响应头
        //设置下载文件的名称
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建Excel文件
        //HSSFWorkbook wb = new HSSFWorkbook();
       /* Workbook wb = employeeService.exportXls();
        //把Excel的数据输出鬼浏览器
        wb.write(response.getOutputStream());*/

       //使用抽取工具阿里的esayexcel
        //查询员工数据
        List<Employee> employees = employeeService.listAll();
        EasyExcel.write(response.getOutputStream(),Employee.class).sheet("员工名单").doWrite(employees);

    }
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {//调用文件上传,参数file必须是跟浏览器的一致
        //System.out.println(file.getOriginalFilename());
        employeeService.inportXls(file);
        return new JsonResult();
//昨晚这部之后会报错空指针异常,所以需要在mvc.xml中配置文件上传解析器MultipartFile
        //还有要添加上传文件的依赖
    }
}
